package com.whfc.ms.api.fse.service.impl;

import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.fse.dto.FseCraneDTO;
import com.whfc.fse.dto.FseCraneFvsDeviceDTO;
import com.whfc.fse.dto.FseCraneToolDTO;
import com.whfc.fse.param.*;
import com.whfc.fse.service.FseCraneService;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.service.FvsDeviceService;
import com.whfc.ms.api.fse.service.MsFseCraneService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @DESCRIPTION
 * @AUTHOR GuoDong_Sun
 * @DATE 2020/4/8
 */
@Service
public class MsFseCraneServiceImpl implements MsFseCraneService {

    @DubboReference(interfaceClass = FseCraneService.class, version = "1.0.0")
    private FseCraneService fseCraneService;

    @DubboReference(interfaceClass = FvsDeviceService.class, version = "1.0.0")
    private FvsDeviceService fvsDeviceService;

    @Override
    public PageData<FseCraneDTO> list(Integer pageNum, Integer pageSize, Integer deptId, String keyword, Integer bindFlag, Integer netState) {
        return fseCraneService.list(pageNum, pageSize, deptId, keyword, bindFlag, netState);
    }

    @Override
    public void add(FseCraneAddParam request) {
        fseCraneService.add(request);
    }

    @Override
    public void edit(FseCraneEditParam request) {
        fseCraneService.edit(request);
    }

    @Override
    public void del(Integer id) {
        fseCraneService.del(id);
    }

    @Override
    public void bind(FseCraneBindParam request) {
        fseCraneService.bind(request);
    }

    @Override
    public void unbind(Integer craneId) {
        fseCraneService.unbind(craneId);
    }

    @Override
    public ListData<FseCraneDTO> list(Integer deptId, Integer bindFlag) {
        return fseCraneService.list(deptId, bindFlag);
    }

    @Override
    public FseCraneDTO detail(Integer craneId) {
        return fseCraneService.detail(craneId);
    }

    @Override
    public List<FseCraneFvsDeviceDTO> fvsList(Integer craneId) {
        List<FseCraneFvsDeviceDTO> list = fseCraneService.getFvsDeviceList(craneId);
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }
        //取出设备ID 查找监控设备信息
        Map<Integer, FseCraneFvsDeviceDTO> map = new HashMap<>();
        for (FseCraneFvsDeviceDTO fseCraneFvsDeviceDTO : list) {
            map.put(fseCraneFvsDeviceDTO.getFvsDeviceId(), fseCraneFvsDeviceDTO);
        }
        List<FvsDeviceDTO> fvsDeviceList = fvsDeviceService.list(new ArrayList<>(map.keySet()));
        for (FvsDeviceDTO fvsDeviceDTO : fvsDeviceList) {
            FseCraneFvsDeviceDTO fseCraneFvsDeviceDTO = map.get(fvsDeviceDTO.getId());
            if (fseCraneFvsDeviceDTO != null) {
                fseCraneFvsDeviceDTO.setFvsDevice(fvsDeviceDTO);
            }
        }
        return new ArrayList<>(map.values());
    }

    @Override
    public void bindFvs(FseCraneFvsParam request) {
        fseCraneService.bindFvs(request);
    }

    @Override
    public PageData<FseCraneToolDTO> getCraneToolList(Integer deptId, Integer pageNum, Integer pageSize) {
        return fseCraneService.getCraneToolList(deptId, pageNum, pageSize);
    }

    @Override
    public void addCraneTool(FseCraneToolAdd param) {
        fseCraneService.addCraneTool(param);
    }

    @Override
    public void editCraneTool(FseCraneToolEdit param) {
        fseCraneService.editCraneTool(param);
    }

    @Override
    public void delCraneTool(Integer toolId) {
        fseCraneService.delCraneTool(toolId);
    }
}
